package com.wpx.fileSys.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import org.json.JSONArray;

import com.wpx.fileSys.entity.FileList;

public class FileOperation{
//	yys --阴阳师账号
//	yysSR --阴阳师SSR卡
//	yysZH --阴阳师自抽号
//	J --诛仙金币
//	Z --诛仙账号
	
	public static String filePath="/Users/jianghao/Documents/server/";
//	public static String filePath="C://web//";
	
	public static void insertFileDatas(FileList file) {
		
		String sql="";
		String split="";
		int sum=0;
		switch (file.getFile_type()) {
		case "yys":
			sql = "insert into data_yys (yys_filename,yys_username, yys_account,yys_pwd) "
					+ "values(?,?,?,?);";
			sum=2;
			split="----";
			break;
		case "yysSR":
			sql = "insert into data_yysSR (yysSR_filename,yysSR_username,yysSR_num,yysSR_platform,yysSR_area, yysSR_account,yysSR_pwd,yysSR_SSR,yysSR_amount) "
					+ "values(?,?,?,?,?,?,?,?,?);";
			sum=7;
			split="----";
			break;
		case "yysZH":
			sql = "insert into data_yysZH (yysZH_filename,yysZH_username, yysZH_num,yysZH_area,yysZH_nickname,yysZH_account,yysZH_pwd,yysZH_amount) "
					+ "values(?,?,?,?,?,?,?,?);";
			sum=6;
			split="----";
			break;
		case "j":
			sql = "insert into data_j (j_filename,j_username, j_account,j_pwd,j_coin,j_nickname,j_area,j_level,j_time) "
					+ "values(?,?,?,?,?,?,?,?,?);";
			sum=7;
			split="---";

			break;
		case "z":
			sql = "insert into data_z (z_filename,z_username, z_account,z_pwd) "
					+ "values(?,?,?,?);";
			sum=2;
			split="---";
			break;
		default:
			
			return;	
		}
		
		file.setFile_sql(sql);
		file.setFile_num(sum);
		file.setFile_split(split);
		insertFile(file);
		
	}
	
	
	private static void insertFile(FileList file) {
		
		Connection connection = JdbcUtils.getConnection();		
		PreparedStatement preparedStatement = null;
		try {
			connection.setAutoCommit(false);
			
			preparedStatement = connection.prepareStatement(file.getFile_sql());

			File fileData = new File(filePath+file.getFile_dir()+"/"+file.getFile_name());

			if (fileData.isFile() && fileData.exists()&&!file.getFile_name().matches(".*密码.*")) {
				
				InputStreamReader read = null;
				read = new InputStreamReader( new FileInputStream(fileData), "utf8");
				BufferedReader bufferedReader = new BufferedReader(read); 
				String lineTXT = null; 
				
				while ((lineTXT = bufferedReader.readLine()) != null) { 
					String [] stuAttr=lineTXT.toString().split(file.getFile_split());
					
					preparedStatement.setString(1, file.getFile_name());
					preparedStatement.setString(2, file.getFile_dir());
					
					for (int i = 0; i < stuAttr.length; i++) {
						preparedStatement.setString(i+3, stuAttr[i]);
					}
					for (int i = stuAttr.length; i < file.getFile_num(); i++) {
						preparedStatement.setObject(i+3, "0");
					}
					preparedStatement.addBatch();
					
				}
				
				preparedStatement.executeBatch();
				connection.commit();
				read.close();
				
				
			}else{ 
				System.out.println("找不到指定的文件！"+fileData.getAbsolutePath()); 
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
			System.out.println("error 数据插入失败");
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			System.out.println("error 数据插入失败");
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			System.out.println("error 数据插入失败");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("error 数据插入失败");
		} finally {
			JdbcUtils.close(null, preparedStatement, connection);
		}
		
	}
	

	public static JSONArray fileTitles(String filename) {
		// TODO Auto-generated method stub
//		yys --阴阳师账号
//		yysSR --阴阳师SSR卡
//		yysZH --阴阳师自抽号
//		J --诛仙金币
//		Z --诛仙账号
		JSONArray titles=new JSONArray();
		titles.put("序号");
		switch (filename) {
		case "yys":
			titles.put("账号");
			titles.put("密码");
			break;
		case "yysSR":
			titles.put("编号");
			titles.put("平台");
			titles.put("大区");
			titles.put("账号");
			titles.put("密码");
			titles.put("SSR");
			titles.put("数量");
			break;
		case "yysZH":
			titles.put("编号");
			titles.put("大区");
			titles.put("角色");
			titles.put("账号");
			titles.put("密码");
			titles.put("数量");
			break;
		case "j":
			titles.put("账号");
			titles.put("密码");
			titles.put("编号");
			titles.put("角色");
			titles.put("大区");
			titles.put("等级");
			titles.put("日期");

			break;
		case "z":
			titles.put("账号");
			titles.put("密码");
			break;

		default:
			break;
		}
		titles.put("文件名");
		return titles;
	}
	

	public static String fileType(String fileName) {
		// TODO Auto-generated method stub
//		yys --阴阳师账号
//		yysSR --阴阳师SSR卡
//		yysZH --阴阳师自抽号
//		J --诛仙金币
//		Z --诛仙账号
		String result="other";
		if (fileName.indexOf("yys")>=0) {
			if (fileName.indexOf("yysSR")>=0) {
				result="yysSR";
			}else if(fileName.indexOf("yysZH")>=0){
				result="yysZH";
			}else {
				result="yys";
			}
			
		}else if (fileName.substring(0, 1).equals("j")) {
			result="j";
		}else if (fileName.substring(0, 1).equals("z")) {
			result="z";
		}
		return result;

	}
	
	
}
